﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Com.UI.Web;
using System.Web.UI.WebControls;
using Com.DAO;
using System.Collections;

namespace WF.Common
{
    public class DataGridUtils
    {
        protected static ArrayList actionList = new ArrayList(5);
        //protected static string appPath = System.Configuration.ConfigurationManager.AppSettings["CCOOWebUrl"].ToString();


        public DataGridUtils()
        {
        }


        #region 设置datagridEx的样式

        /// <summary>
        /// 设置页面上的DataGridEx的DataManager
        /// </summary>
        /// <param name="page"></param>
        /// <param name="dataManager"></param>
        public static void SetDataGridExProperty(System.Web.UI.Page page, DataManager dataManager)
        {
            AlloverControls(page, dataManager);
        }

        /// <summary>
        /// 遍历页面控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="dataManager"></param>
        protected static void AlloverControls(System.Web.UI.Control control, DataManager dataManager)
        {
            foreach (System.Web.UI.Control c in control.Controls)
            {
                if (c is DataGridEx)
                {
                    ((DataGridEx)c).DataManager = dataManager;
                }
                else
                    if (c.HasControls())
                    {
                        AlloverControls(c, dataManager);
                    }
            }
        }

        #endregion 设置datagridEx的样式


        #region 设置DataGrid行选择的样式

        public static void SetDataGridSelectedRowStyle(DataGridItemEventArgs e)
        {
            e.Item.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccccff'");//鼠标所在行显示为红色

            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            e.Item.Attributes["style"] = "Cursor:hand";
        }
        public static void SetDataGridSelectedRowStyle(GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccccff'");//鼠标所在行显示为红色

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            e.Row.Attributes["style"] = "Cursor:hand";
        }
        #endregion 设置DataGrid行选择的样式


        #region 合并单元格

        /// <summary>
        /// 根据起、止列数设置DataGridEx需要合并的单元格

        /// </summary>
        /// <param name="control"></param>
        public static void SpanGrid(DataGridEx dgList, int start, int end)
        {
            int i;
            int j;
            int intSpan;
            string strCurrentText, strLastText;

            for (i = 0; i <= dgList.Items.Count - 1; i++)
            {
                intSpan = 1;
                strLastText = "";
                for (int k = start; k <= end; k++)
                {
                    strLastText += dgList.Items[i].Cells[k].Text;
                }
                //循环判断当前行中是否有与上一行相同的内容
                for (j = i + 1; j <= dgList.Items.Count - 1; j++)
                {
                    strCurrentText = "";
                    for (int k = start; k <= end; k++)
                    {
                        strCurrentText += dgList.Items[j].Cells[k].Text;
                    }

                    if (string.Compare(strLastText, strCurrentText) == 0)
                    {
                        intSpan += 1;

                        for (int k = start; k <= end; k++)
                        {
                            dgList.Items[i].Cells[k].RowSpan = intSpan;

                            //把内容相同单元格隐藏 
                            dgList.Items[j].Cells[k].Visible = false;
                        }
                    }
                    else
                    {
                        break; // Exit For 
                    }
                }
                i = j - 1;
            }

        }


        /// <summary>
        /// 根据起、止列数设GridViewEx需要合并的单元格

        /// </summary>
        /// <param name="control"></param>
        public static void SpanGrid(GridViewEx dgList, int start, int end)
        {
            int i;
            int j;
            int intSpan;
            string strCurrentText, strLastText;

            for (i = 0; i <= dgList.Rows.Count - 1; i++)
            {
                intSpan = 1;
                strLastText = "";
                for (int k = start; k <= end; k++)
                {
                    strLastText += dgList.Rows[i].Cells[k].Text;
                }
                //循环判断当前行中是否有与上一行相同的内容
                for (j = i + 1; j <= dgList.Rows.Count - 1; j++)
                {
                    strCurrentText = "";
                    for (int k = start; k <= end; k++)
                    {
                        strCurrentText += dgList.Rows[j].Cells[k].Text;
                    }

                    if (string.Compare(strLastText, strCurrentText) == 0)
                    {
                        intSpan += 1;

                        for (int k = start; k <= end; k++)
                        {
                            dgList.Rows[i].Cells[k].RowSpan = intSpan;

                            //把内容相同单元格隐藏 
                            dgList.Rows[j].Cells[k].Visible = false;
                        }
                    }
                    else
                    {
                        break; // Exit For 
                    }
                }
                i = j - 1;
            }

        }


        /// <summary>
        /// 根据起、止列数设置DataGridEx需要合并的单元格

        /// </summary>
        /// <param name="control"></param>
        protected void SpanGrid(Centaline.WebControl.CentaDataGrid dgList, int start, int end)
        {
            int i;
            int j;
            int intSpan;
            string strCurrentText, strLastText;

            for (i = 0; i <= dgList.Items.Count - 1; i++)
            {
                intSpan = 1;
                strLastText = "";
                for (int k = start; k <= end; k++)
                {
                    strLastText += dgList.Items[i].Cells[k].Text;
                }
                //循环判断当前行中是否有与上一行相同的内容
                for (j = i + 1; j <= dgList.Items.Count - 1; j++)
                {
                    strCurrentText = "";
                    for (int k = start; k <= end; k++)
                    {
                        strCurrentText += dgList.Items[j].Cells[k].Text;
                    }

                    if (string.Compare(strLastText, strCurrentText) == 0)
                    {
                        intSpan += 1;

                        for (int k = start; k <= end; k++)
                        {
                            dgList.Items[i].Cells[k].RowSpan = intSpan;

                            //把内容相同单元格隐藏 
                            dgList.Items[j].Cells[k].Visible = false;
                        }
                    }
                    else
                    {
                        break; // Exit For 
                    }
                }
                i = j - 1;
            }

        }

        #endregion 合并单元格


        #region 导出网格内容到EXCEL
        //对于centadatagrid的导出

        public static void ExportGridDataToExcel(Centaline.WebControl.CentaDataGrid dgData, string excelFileName)
        {
            //如果文件名为中文，必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            bool allowPaging, allowSorting, pagerStyleVisible;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //curContext.Response.ContentEncoding   =   System.Text.Encoding.GetEncoding("GB2312");

                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                //对于排序的datagridex,先要关掉分页和排序功能 2007.07.13 Added by liuyj.ce
                allowPaging = dgData.AllowPaging;
                allowSorting = dgData.AllowSorting;
                pagerStyleVisible = dgData.PagerStyle.Visible;//原来GRID的属性,moidified by liu on 2007.07.30

                dgData.AllowPaging = false;
                dgData.AllowSorting = false;
                dgData.PagerStyle.Visible = false;
                dgData.DataBind();

                dgData.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();

                dgData.AllowPaging = allowPaging;
                dgData.AllowSorting = allowSorting;
                dgData.PagerStyle.Visible = pagerStyleVisible;
                dgData.DataBind();
            }

        }

        public static void ExportDataGridExToExcelEh(DataGridEx dgData, string excelFileName)
        {
            //如果文件名为中文，必须编码为UTF8
            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //curContext.Response.ContentEncoding   =   System.Text.Encoding.GetEncoding("GB2312");

                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                strWriter = new System.IO.StringWriter(sb);
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

                System.Web.UI.Page page = new System.Web.UI.Page();
                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();

                dgData.EnableViewState = false;
                page.EnableEventValidation = false;
                page.DesignerInitialize();

                page.Controls.Add(form);
                form.Controls.Add(dgData);

                page.RenderControl(htmlWriter);
                curContext.Response.Write(sb.ToString());
                curContext.Response.End();
            }

        }


        //ModiedBy Liuyongjun on 2007.07.13
        public static void ExportDataGridExToExcel(Com.UI.Web.DataGridEx dgData, string excelFileName)
        {
            //如果文件名为中文，必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            bool allowPaging, allowSorting, pagerStyleVisible;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //curContext.Response.ContentEncoding   =   System.Text.Encoding.GetEncoding("GB2312");

                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                //对于排序的datagridex,先要关掉分页和排序功能 2007.07.13 Added by liuyj.ce
                allowPaging = dgData.AllowPaging;
                allowSorting = dgData.AllowSorting;
                pagerStyleVisible = dgData.PagerStyle.Visible;//原来GRID的属性,moidified by liu on 2007.07.30

                if (allowPaging)
                    dgData.AllowPaging = false;
                if (allowSorting)
                    dgData.AllowSorting = false;
                if (pagerStyleVisible)
                    dgData.PagerStyle.Visible = false;
                if (allowPaging || allowSorting || pagerStyleVisible)
                    dgData.BindData();

                dgData.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
                if (allowPaging)
                    dgData.AllowPaging = allowPaging;
                if (allowSorting)
                    dgData.AllowSorting = allowSorting;
                if (pagerStyleVisible)
                    dgData.PagerStyle.Visible = pagerStyleVisible;
                if (allowPaging || allowSorting || pagerStyleVisible)
                    dgData.BindData();
                dgData.BindData();
            }
        }

        //ModiedBy Liuyongjun on 2007.07.13
        public static void ExportDataGridExToExcelWithDataSet(Com.UI.Web.DataGridEx dgData, string excelFileName)
        {
            //如果文件名为中文，必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;

                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

                dgData.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();

            }
        }


        public static void ExportDataGridExToExcel(Com.UI.Web.DataGridEx dgData, string excelFileName, bool bIsGB2312)
        {
            //如果文件名为中文，必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            bool allowPaging, allowSorting, pagerStyleVisible;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                if (bIsGB2312)
                {
                    curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                }
                else
                {
                    curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;

                }
                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                //对于排序的datagridex,先要关掉分页和排序功能 2007.07.13 Added by liuyj.ce
                allowPaging = dgData.AllowPaging;
                allowSorting = dgData.AllowSorting;
                pagerStyleVisible = dgData.PagerStyle.Visible;//原来GRID的属性,moidified by liu on 2007.07.30

                dgData.AllowPaging = false;
                dgData.AllowSorting = false;
                dgData.PagerStyle.Visible = false;
                dgData.BindData();

                dgData.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();

                dgData.AllowPaging = allowPaging;
                dgData.AllowSorting = allowSorting;
                dgData.PagerStyle.Visible = pagerStyleVisible;
                dgData.BindData();
            }
        }


        public static void ExportDataGridExToExcel(Com.UI.Web.DataGridEx dgData, System.Web.UI.HtmlControls.HtmlTable ultraChart, string excelFileName, bool bIsGB2312)
        {
            //如果文件名为中文，必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            bool allowPaging, allowSorting, pagerStyleVisible;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                if (bIsGB2312)
                {
                    curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                }
                else
                {
                    curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;

                }
                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                //对于排序的datagridex,先要关掉分页和排序功能 2007.07.13 Added by liuyj.ce
                allowPaging = dgData.AllowPaging;
                allowSorting = dgData.AllowSorting;
                pagerStyleVisible = dgData.PagerStyle.Visible;//原来GRID的属性,moidified by liu on 2007.07.30

                dgData.AllowPaging = false;
                dgData.AllowSorting = false;
                dgData.PagerStyle.Visible = false;
                dgData.BindData();

                //dgData.RenderControl(htmlWriter);  
                ultraChart.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();

                dgData.AllowPaging = allowPaging;
                dgData.AllowSorting = allowSorting;
                dgData.PagerStyle.Visible = pagerStyleVisible;
                dgData.BindData();
            }
        }


        #endregion


        /// <summary>
        /// 设置操作列

        /// </summary>
        /// <param name="cell">操作所在列</param>
        /// <param name="name">操作名称</param>
        /// <param name="operateNo">操作代码</param>
        /// <param name="url">操作响应页面URL</param>
        /// <param name="keyName">主键名</param>
        /// <param name="keyCell">主键所在列</param>
        /// <param name="isConfirm">是否弹出确认窗口</param>
        public static void SetAction(int cell, string name, string operateNo, string url, string keyName, int keyCell, bool isConfirm, string features)
        {
            ArrayList action = new ArrayList(10);
            action.Add(cell);
            action.Add(name);
            action.Add(operateNo);
            action.Add(url);
            action.Add(keyName);
            action.Add(keyCell);
            action.Add(isConfirm);
            action.Add(false);	//isChinese
            action.Add(features);
            actionList.Add(action);
        }

        /// <summary>
        /// 设置操作列

        /// </summary>
        /// <param name="cell">操作所在列</param>
        /// <param name="name">操作名称</param>
        /// <param name="operateNo">操作代码</param>
        /// <param name="url">操作响应页面URL</param>
        /// <param name="keyName">主键名</param>
        /// <param name="keyCell">主键所在列</param>
        /// <param name="isConfirm">是否弹出确认窗口</param>
        /// <param name="isKeyChinese">主键值是否中文值</param>
        public static void SetAction(int cell, string name, string operateNo, string url, string keyName, int keyCell, bool isConfirm, bool isKeyChinese)
        {
            ArrayList action = new ArrayList(10);
            action.Add(cell);
            action.Add(name);
            action.Add(operateNo);
            action.Add(url);
            action.Add(keyName);
            action.Add(keyCell);
            action.Add(isConfirm);
            action.Add(isKeyChinese);

            actionList.Add(action);
        }

        /// <summary>
        /// 清除ActionList
        /// </summary>
        public static void ClearDataGridAction()
        {
            actionList.Clear();
        }

        /// <summary>
        /// 在DateGrid的ItemDataBound事件中调用

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DataGridItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemIndex >= 0)
                {
                    DataGridUtils.SetDataGridSelectedRowStyle(e);
                    for (int i = 0; i < actionList.Count; i++)
                    {
                        ArrayList action = (ArrayList)actionList[i];
                        int cell = Convert.ToInt32(action[0]);
                        string name = action[1].ToString();
                        string operateNo = action[2].ToString();
                        string url = action[3].ToString();
                        string keyName = action[4].ToString();
                        int keyCell = Convert.ToInt32(action[5]);
                        bool isCheck = Convert.ToBoolean(action[6]);
                        bool isKeyChinese = Convert.ToBoolean(action[7]);
                        string feature = action[8].ToString();

                        e.Item.Cells[cell].CssClass = "tdActionOut";
                        e.Item.Cells[cell].Attributes.Add("onMouseOver", "this.className='tdActionOver';");
                        e.Item.Cells[cell].Attributes.Add("onMouseOut", "this.className='tdActionOut';");

                        string u = "";
                        if (url.IndexOf("?") > -1)
                        {
                            u = url + "&";
                        }
                        else
                        {
                            u = url + "?";
                        }
                        //在url后加入一随机数以防止缓存
                        Random random = new Random();
                        if (isKeyChinese)
                        {
                            u = u + "CmdType=" + name + "&" + keyName + "=" + System.Web.HttpUtility.UrlEncode(e.Item.Cells[keyCell].Text) + "&" + random.Next(0, 1000).ToString();
                        }
                        else
                        {
                            u = u + "CmdType=" + name + "&" + keyName + "=" + System.Web.HttpUtility.UrlEncode(e.Item.Cells[keyCell].Text) + "&" + random.Next(0, 1000).ToString();
                        }

                        if (isCheck)
                        {
                            e.Item.Cells[cell].Attributes.Add("onclick", "if(confirm(\"是否继续执行该操作？\")){OpenAction(\"" + u + "\",\"" + feature + "\");return false; } else{return false;}");
                        }
                        else
                        {
                            e.Item.Cells[cell].Attributes.Add("onclick", "OpenAction(\"" + u + "\",\"" + feature + "\"); return false;");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 在Asp.net 2.0中的GridView 的RowDataBound事件中调用

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void GridViewRowDataBound(object sender, System.Web.UI.WebControls.GridView gridView, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowIndex >= 0)
                {
                    DataGridUtils.SetDataGridSelectedRowStyle(e);
                    for (int i = 0; i < actionList.Count; i++)
                    {
                        ArrayList action = (ArrayList)actionList[i];
                        int cell = Convert.ToInt32(action[0]);
                        string name = action[1].ToString();
                        string operateNo = action[2].ToString();
                        string url = action[3].ToString();
                        string keyName = action[4].ToString();
                        int keyCell = Convert.ToInt32(action[5]);
                        bool isCheck = Convert.ToBoolean(action[6]);
                        bool isKeyChinese = Convert.ToBoolean(action[7]);
                        string feature = action[8].ToString();

                        e.Row.Cells[cell].CssClass = "tdActionOut";
                        e.Row.Cells[cell].Attributes.Add("onMouseOver", "this.className='tdActionOver';");
                        e.Row.Cells[cell].Attributes.Add("onMouseOut", "this.className='tdActionOut';");

                        string u = "";
                        if (url.IndexOf("?") > -1)
                        {
                            u = url + "&";
                        }
                        else
                        {
                            u = url + "?";
                        }
                        //在url后加入一随机数以防止缓存
                        Random random = new Random();
                        if (isKeyChinese)
                        {
                            u = u + "CmdType=" + name + "&" + keyName + "=" + System.Web.HttpUtility.UrlEncode(gridView.DataKeys[e.Row.RowIndex][keyName].ToString()) + "&" + random.Next(0, 1000).ToString();
                        }
                        else
                        {
                            u = u + "CmdType=" + name + "&" + keyName + "=" + System.Web.HttpUtility.UrlEncode(gridView.DataKeys[e.Row.RowIndex][keyName].ToString()) + "&" + random.Next(0, 1000).ToString();
                        }

                        if (isCheck)
                        {
                            e.Row.Cells[cell].Attributes.Add("onclick", "if(confirm(\"是否继续执行该操作？\")){OpenAction(\"" + u + "\",\"" + feature + "\");return false; } else{return false;}");
                        }
                        else
                        {
                            e.Row.Cells[cell].Attributes.Add("onclick", "OpenAction(\"" + u + "\",\"" + feature + "\"); return false;");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 增加删除操作列

        /// </summary>
        /// <param name="dataGrid"></param>
        public static void AddDeleteColumn(Com.UI.Web.DataGridEx dataGrid)
        {
            ButtonColumn bc = new ButtonColumn();
            //BoundColumn bc = new BoundColumn();
            bc.HeaderText = "删除";
            //bc.CommandName = "Delete";
            //bc.Text = "<img src='" + this.AppPath + "images/delete.gif" + "' border='0' onclick='return confirm(\"确定删除吗？\")'>";
            bc.Text = "<img src='" + dataGrid.Page.Application["AppPath"].ToString() + "images/delete.gif" + "' border='0' title='删除' alt='删除'>";
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            bc.ItemStyle.Width = Unit.Pixel(30);
            dataGrid.Columns.Add(bc);
        }

        #region 保存DataGrid中选择的记录关键字段值到Sesssion中

        public static void SaveSelectedKeyValueInSession(System.Web.UI.Page page, Com.UI.Web.DataGridEx dgList, string sessionName, int KeyValueCellID, int checkboxCellID, string checkboxControlName)
        {
            ArrayList al = (ArrayList)page.Session[sessionName];
            if (al == null)
            {
                al = new ArrayList();
            }

            for (int i = 0; i < dgList.Items.Count; i++)
            {
                CheckBox ckb = (CheckBox)dgList.Items[i].Cells[checkboxCellID].FindControl(checkboxControlName);
                if (ckb != null)
                {
                    string id = dgList.Items[i].Cells[KeyValueCellID].Text.ToString();
                    if (ckb.Checked)
                    {
                        if (!al.Contains(id))
                        {
                            al.Add(id);
                        }
                    }
                    else
                    {
                        if (al.Contains(id))
                        {
                            al.Remove(id);
                        }
                    }

                }

            }

            page.Session[sessionName] = al;

        }
        #endregion
    }
}
